package com.yang.project.base.set.list;

import org.junit.Test;

import java.util.*;

public class ListTest {
    /**
     * list集合是否可以添加空值
     * 可以添加
     */
    @Test
    public void test() {
        List<String> list = new ArrayList<>();
        list.add(null);
        list.add("张三");
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    /**
     * LinkedList支不支持随机访问
     */
    @Test
    public void test02() {
        List<String> list = new LinkedList<>();
        list.add("张三");
        list.add("张三2");
        list.add("张三3");
        list.add("张三4");
        String s = list.get(2);
        System.out.println(s);
    }


    /**
     * 使用Collections定制排序，里面实现Comparator接口
     */
    @Test
    public void test03() {
        List<String> list = new LinkedList<>();
        list.add("张三");
        list.add("张三2");
        list.add("张三3");
        list.add("张三4");
        System.out.println("原始数组：" + list);
        Collections.reverse(list);
        System.out.println("反转数组：" + list);
        Collections.sort(list);
        System.out.println("自然排序：" + list);
        Collections.sort(list, (o1, o2) -> o2.compareTo(o1));
        System.out.println("定制排序: " + list);
    }

    /**
     * 测试实现Comparable接口排序
     */
    @Test
    public void test04() {
        //使用list不行
//        List<Person> list = new ArrayList<Person>();
//        list.add(new Person("张三", 13));
//        list.add(new Person("张三", 10));
//        list.add(new Person("张三", 9));
//        list.add(new Person("张三", 14));
//        list.add(new Person("张三", 20));
//        Iterator<Person> iterator = list.iterator();
//        while (iterator.hasNext()) {
//            System.out.println(iterator.next());
//        }
        //map的key可以实现这个接口完成排序
        TreeMap<Person, String> map = new TreeMap<>();
        map.put(new Person("张三", 13), "张三");
        map.put(new Person("张三", 10), "张三");
        map.put(new Person("张三", 9), "张三");
        map.put(new Person("张三", 14), "张三");
        map.put(new Person("张三", 20), "张三");
        Set<Person> set = map.keySet();
        for (Person s : set) {
            System.out.println(s);
        }
    }






}
